import 'dart:convert';

import 'package:flutter/material.dart';

import '../../../models/moment/comment_model.dart';
import '../../../providers/user_context.dart';
import '../../../utils/HttpUtil.dart';
import 'comment_area_widget.dart';

class ReplyAreaWidget extends StatefulWidget {
  const ReplyAreaWidget(
      {super.key,
      required this.replies,
      required this.momentId,
      required this.parentId,
      required this.commentInput});

  final Replies replies;
  final int parentId;
  final int momentId;
  final ShowCommentInput commentInput;

  @override
  State<ReplyAreaWidget> createState() => _ReplyAreaWidgetState();
}

class _ReplyAreaWidgetState extends State<ReplyAreaWidget> {
  final _commentController = TextEditingController();
  var likeCnt;
  var hasLike;

  @override
  void initState() {
    likeCnt = widget.replies?.likeCnt?.toInt() ?? 0;
    hasLike = false;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 10.0, top: 5), // 设置左边距为 16.0
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly, // 设置元素之间的间距为均匀分布
        children: [
          Row(
            children: [
              Wrap(
                spacing: 5,
                children: [
                  CircleAvatar(
                    radius: 12,
                    backgroundImage: NetworkImage(
                        widget.replies.userBaseInfo?.avatar ??
                            "https://randomuser.me/api/portraits/men/81.jpg"),
                  ),
                  Text(
                    widget.replies.userBaseInfo?.userName ?? '小老弟:',
                    style: TextStyle(color: Colors.blue),
                  ),
                ],
              ),
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Wrap(
            children: [
              if (widget.replies.replyUserName?.isNotEmpty ?? false)
                const Text(
                  "回复 ",
                  style: TextStyle(fontSize: 16),
                ),
              if (widget.replies.replyUserName?.isNotEmpty ?? false)
                Text(
                  '${widget.replies!.replyUserName!}: ',
                  style: TextStyle(fontSize: 16, color: Colors.grey),
                ),
              Text(
                widget.replies.comment ?? " ",
                style: TextStyle(fontSize: 16),
              )
            ],
          ),
          SizedBox(
            height: 5,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '${widget.replies.dateTime}  ${widget.replies.userPosition ?? "未知位置"}',
                style: TextStyle(fontSize: 12, color: Colors.grey),
              ),
              Container(
                child: Wrap(
                  children: [
                    GestureDetector(
                      onTap: () {
                        widget.commentInput(
                            widget.parentId, widget.replies.commentId!);
                      },
                      child: const Padding(
                        padding: EdgeInsets.only(right: 10),
                        child: Icon(
                          Icons.comment_outlined,
                          size: 18,
                          color: Colors.grey,
                        ),
                      ),
                    ),
                    GestureDetector(
                      onTap: () {
                        if (hasLike) {
                          return;
                        }
                        _like(widget.replies.commentId);
                        setState(() {
                          likeCnt++;
                          hasLike = true;
                        });
                      },
                      child: Padding(
                          padding: EdgeInsets.only(right: 5),
                          child: Wrap(
                            crossAxisAlignment: WrapCrossAlignment.center,
                            children: [
                              Icon(
                                hasLike
                                    ? Icons.favorite
                                    : Icons.favorite_border,
                                size: 18,
                                color: hasLike ? Colors.blue : Colors.grey,
                              ),
                              Text(likeCnt.toString() ?? ""),
                            ],
                          )),
                    ),
                  ],
                ),
              )
            ],
          ),
          SizedBox(
            height: 14,
          )
        ],
      ),
    );
  }

  void _like(num? commentId) async {
    var response = await HttpUtil.getInstance().post("/api/comment/v1/like",
        data: jsonEncode({
          'creatorId': UserContext.cxt()!.uid,
          'momentId': widget.momentId,
          'commentId': commentId
        }));

    if (response!.statusCode == 200) {
      print("评论成功");
    } else {
      print("s");
    }
  }

  @override
  void dispose() {
    super.dispose();
    _commentController.dispose();
  }
}
